<template lang="">
    <div>
        <el-form label-width="80px" :model="ques" :rules="rule" ref="ques">
            <el-form-item label="题目" prop="queQue">
                <el-input v-model="ques.queQue" @keyup.enter.native='emitServer'></el-input>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'FillUpdate',
        data() {
            return {
                ques: {
                    queQue: '',
                },
                rule: {
                    queQue: { required: true, message: '请输入题目', trigger: 'blur' }
                },
                isAdding: false,
                addOptText: '',
            }
        },
        props: ['question','isEditQues'],
        methods: {
            elementMessage(result) {
                if (result == 0) {
                    this.$message({
                        message: '修改失败',
                        type: 'error'
                    });
                } else {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                }
            },
            emitServer() {
                this.$refs['ques'].validate((valid) => {
                    if (valid) {



                        if (this.isEditQues == true) {                                        //根据环境切换url发送位置
                            var url = '/question/updateQuestion'
                        } else if (this.isEditQues == false) {
                            var url = '/questionBank/updateQuestion'
                        }



                        console.log(this.ques);
                        let ques = this.ques
                        axios({
                            method: 'POST',
                            url: url,
                            withCredentials: true,
                            data: JSON.stringify(ques),
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }).then((result) => {
                            this.elementMessage(result.data)
                            this.$bus.$emit('addQuesSuccess')   //告知添加成功
                        }).catch((err) => {
                            this.$message(err.message);
                        });

                    } else {
                        return false;
                    }
                });
            },
        },
        mounted() {
            this.$bus.$on('updateFill', () => {
                this.emitServer()
            })
            this.ques = this.question
        },
        beforeDestroy() {
            this.$bus.$off('updateFill')
        },
    }
</script>
<style lang="">

</style>